package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.service.ISysGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 二手物品Controller
 * 
 * @author jjp
 * @date 2020-04-21
 */
@RestController
@RequestMapping("/system/goods")
public class SysGoodsController extends BaseController
{
    @Autowired
    private ISysGoodsService sysGoodsService;

    /**
     * 后台查询二手物品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGoods sysGoods)
    {
        startPage();
        List<SysGoods> list = sysGoodsService.selectSysGoodsLists(sysGoods);
        return getDataTable(list);
    }

    /**
     * 获取二手物品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(sysGoodsService.selectSysGoodsById(goodsId));
    }

    /**
     * 登录之后
     * 用户新增二手物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "二手物品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGoods sysGoods)
    {
        return toAjax(sysGoodsService.insertSysGoods(sysGoods));
    }


    /**
     * 后台审核二手物品
     */
    @Log(title = "二手物品", businessType = BusinessType.UPDATE)
    @PutMapping("updateSysGoods")
    @RepeatSubmit
    public AjaxResult updateSysGoods(@RequestBody SysGoods sysGoods)
    {
        String examineStatus = sysGoods.getExamineStatus();
        String reason = sysGoods.getReason();
        if(examineStatus.equals("1")){
            sysGoods.setExamineStatus(examineStatus);
            sysGoodsService.updateSysGoods(sysGoods);
        }

        if(examineStatus.equals("2")){
            sysGoods.setExamineStatus(examineStatus);
            sysGoods.setReason(reason);
            sysGoodsService.updateSysGoods(sysGoods);
            return AjaxResult.success(reason);
        }

        return AjaxResult.success();
    }

    /**
     * 修改二手物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "二手物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGoods sysGoods)
    {
        return toAjax(sysGoodsService.updateSysGoods(sysGoods));
    }

    /**
     * 删除二手物品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "二手物品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(sysGoodsService.deleteSysGoodsByIds(goodsIds));
    }
}
